package com.shadow.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * @author zhangyu
 * @date 2018/1/5
 */

public class DateDialogUtil {
    public interface IDateListener {
        void onDateChange(String date);
    }

    public static void showDialog(Context context, final IDateListener listener) {
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        final DatePicker datePicker = new DatePicker(context);
        final String[] mSelectDate = new String[1];
        datePicker.init(1990, 1, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int mouth, int day) {
                final Calendar calendar = Calendar.getInstance();
                calendar.set(year, mouth, day);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年mm月dd日", Locale.getDefault());
                mSelectDate[0] = dateFormat.format(calendar.getTime());
            }
        });
        datePicker.setLayoutParams(params);
        linearLayout.addView(datePicker);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(linearLayout)
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (listener != null && mSelectDate[0] != null) {
                            listener.onDateChange(mSelectDate[0]);
                        }
                    }
                })
                .setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .show();

    }
}
